package com.sysu.dinner.action;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.dinner.service.UserService;

@Component("loginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport{

	private static final long serialVersionUID = -8467090050232329238L;
	
	private String name;
	private String password;
	private String phone;
	
	private UserService userService;

	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=GBK");
		PrintWriter out = response.getWriter();
		
		if(name!=null && password != null && phone != null && userService.isVaild(name, password, phone)){
			HttpSession session = ServletActionContext.getRequest().getSession();;
			session.setAttribute("user", name);
			session.setAttribute("role", "user");
			session.setAttribute("phone", phone);
			out.println(1);
		}else {
			out.println(2);
		}
		
		return null;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPhone() {
		return phone;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	
}
